package com.xbjtech.service.impl;

import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xbjtech.mapper.HxmMapper;
import com.xbjtech.model.Hxm;
import com.xbjtech.service.HxmRandomService;

@Service
public class HxmRandomServiceImpl implements HxmRandomService {

	@Autowired
	HxmMapper hxmMapper;
	
	/**
	 * 产生具有一个唯一性的9位数字的字符串
	 * eg.   001002322
	 * 		 123456789
	 *       987654321
	 */
	@Override
	public String random() {
		synchronized(this) {
			String code = randonNiceCharString();
			Hxm hxm = hxmMapper.getHxmByHxmstring(code);
			if (null == hxm) {
				hxm = new Hxm();
				hxm.setHxm(code);
				hxmMapper.insertHxm(hxm);
				return code;
			} 
			return random();
		}
	}
	
	private String randonNiceCharString() {
		int[] dis = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
		StringBuffer sb = new StringBuffer();
		for (int i = 1; i < dis.length; i++) {
			sb.append(dis[new Random().nextInt(dis.length)]);
		}
		return sb.toString();
	}

	
	
}
